package com.nextbus.parser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.nextbus.model.VehicleLocationsData;
import com.nextbus.util.Constants;

public class VehicleLocationsParser extends DefaultHandler{
	private ArrayList<VehicleLocationsData> vehicleLocationsDatas=new ArrayList<VehicleLocationsData>();
	private String id=null;			//车的id号
	private String routeTag=null;	//路线名称
	private String dirTag=null; 	//方向名称
	private String lat=null;
	private String lon=null;
	private String secsSinceReport=null;//距离报告已经有几秒了
	private String predictable=null;
	private String heading=null;	//车开的方向，0——360里面有个指向的角度
	private String speedKmHr=null;	//速度
	
	private String direction;
	
	public  ArrayList<VehicleLocationsData> getVehicleLocationsDatas() {		
		return vehicleLocationsDatas;
	}
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
	}

	public void endDocument() throws SAXException {
		super.endDocument();
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
	}

	public void startDocument() throws SAXException {
		super.startDocument();
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		//dxt   -----------------这个解析需要一一对应过去，如果没有某项就默认为null，这样不会混乱-------
		if (localName.equals("vehicle")) {
			this.id=attributes.getValue(Constants.id);
			this.routeTag=attributes.getValue(Constants.routeTag);
			this.dirTag=attributes.getValue(Constants.dirTag);
			this.lat=attributes.getValue(Constants.lat);
			this.lon=attributes.getValue(Constants.lon);
			this.secsSinceReport=attributes.getValue(Constants.secsSinceReport);
			this.predictable=attributes.getValue(Constants.predictable);
			this.heading=attributes.getValue(Constants.heading);
			this.speedKmHr=attributes.getValue(Constants.speedKmHr);
			
			//dxt 仅仅解析这个方向上的车，速度快一点
			if (dirTag!=null) {
				if (dirTag.equals(direction)) {
				VehicleLocationsData vehicleLocationsData=new VehicleLocationsData(
					id, routeTag, dirTag, lat, lon, secsSinceReport, predictable, heading, speedKmHr);
				vehicleLocationsDatas.add(vehicleLocationsData);
				}
			}
			setNull();
			
		}
		super.startElement(uri, localName, qName, attributes);
	}
	
	private void setNull() {
		this.routeTag=null;
		this.dirTag=null;
		this.lat=null;
		this.lon=null;
		this.id=null;			//车的id号
		this.secsSinceReport=null;//距离报告已经有几秒了
		this.predictable=null;
		this.heading=null;	//车开的方向，0——360里面有个指向的角度
		this.speedKmHr=null;	//速度
	}
	public void setDirection(String direction) {
		// TODO Auto-generated method stub
		this.direction=direction;
	}
}
